package com.sense.kukoo.biz_me.view

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.sense.kukoo.biz_me.R
import com.sense.kukoo.biz_me.databinding.BizMeActivityVerifyCodeBinding
import com.sense.kukoo.biz_me.vm.VerifyCodeViewModel
import com.sense.kukoo.common.constant.ARouterUrl
import com.sense.kukoo.common.mvvm.view.BaseMvvmActivity
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.utils.CountDownerUtil
import com.sense.kukoo.common.utils.KeyBoardUtils
import com.sense.kukoo.common.wiget.VerifiyCodeEditText
import com.sense.kukoo.lib_logger.LoggerManager

@Route(path = ARouterUrl.ROUTE_ME_MODULE_VERIFY_CODE_ACTIVITY)
class VerifyCodeActivity : BaseMvvmActivity<VerifyCodeViewModel, BizMeActivityVerifyCodeBinding>() {

    private val TAG: String = javaClass.simpleName

    @JvmField
    @Autowired(name = ARouterUrl.KEY_AREA_CODE)
    var from_areaCode: String? = null

    @JvmField
    @Autowired(name = ARouterUrl.KEY_PHONE_NUMBER)
    var from_phoneNumber: String? = null

    override fun getLayoutId(): Int = R.layout.biz_me_activity_verify_code

    override fun getViewModelClass(): Class<VerifyCodeViewModel> = VerifyCodeViewModel::class.java

    override fun responseVM(action: Int) {}

    override fun initData() {
        ARouter.getInstance().inject(this)
        //
        mBinding.lifecycleOwner = this
        mBinding.vm = mViewModel
        mBinding.titleView.init(this, "", false)
        setSupportActionBar(mBinding.titleView)
        //
        mViewModel.setFromData(from_areaCode!!,from_phoneNumber!!)
        //
        bindData(mViewModel.mIsShowPhoneErrorHit){
            mBinding.tvPhoneErrorHint.visibility = if(it) View.VISIBLE else View.GONE
        }
        bindData(mViewModel.mIsShowSentTime){
            mBinding.tvSentTimer.visibility = if(it) View.VISIBLE else View.GONE
        }
        bindData(mViewModel.mIsShowRecent){
            mBinding.tvBtnRecend.visibility = if(it) View.VISIBLE else View.GONE
        }
        bindAction(mViewModel.action_toCountDownTimer){
            doCountDownTimer()
        }
        bindAction(mViewModel.action_toLoginSuccess){
            finish()
        }
        bindData(mViewModel.mBtnEnable){
            if(it){
                mBinding.ivBtnEnter.setImageResource(R.drawable.biz_me_icon_enter_phone_enabled)
            }else{
                mBinding.ivBtnEnter.setImageResource(R.drawable.biz_me_icon_enter_phone_disable)
            }
        }
    }

    override fun initView() {
        //
        val sent_to = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.login_verify_code_sent_to, from_areaCode, from_phoneNumber)
        mBinding.tvSentTo.text = sent_to
        //
        mBinding.etVerifyCode.setOnPasswordListener(object :VerifiyCodeEditText.OnPasswordListener{
            override fun onSuccess(text: String?) {
                mViewModel.onVerifyCodeChanged(text)
            }

            override fun onEnter(text: String?) {
                mViewModel.onVerifyCodeChanged(text)
            }
        })
        //
        mBinding.etVerifyCode.requestFocus()
        KeyBoardUtils.openKeybord(mBinding.etVerifyCode, this)
        //
        mViewModel.initData()
    }

    override fun onPause() {
        super.onPause()
        //
        if (isFinishing) {
            mBinding.etVerifyCode.clearFocus()
            KeyBoardUtils.closeKeybord(mBinding.etVerifyCode, this)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        //
        mBinding.etVerifyCode.clearFocus()
        KeyBoardUtils.closeKeybord(mBinding.etVerifyCode, this)
    }

    fun doCountDownTimer(){
        val timer = CountDownerUtil(this, 60000, 1000)
        timer.setOnFinishListenrer(object :CountDownerUtil.OnFinishListener{
            override fun onFinish() {
                mViewModel.onCountDownTimerFinish()
            }

            override fun onTick(millisUntilFinished: Long) {
                System.currentTimeMillis()
                val second = millisUntilFinished/1000
                val strs = if(second < 10){ "0" + second} else second
                val str = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.login_verify_error_hint_2, strs)
                mBinding.tvSentTimer.text = str
            }
        })
        timer.start()
    }
}